#include "stdafx.h"
#include "Vector3.h"


Vector3::Vector3( int n, float a[]) {
	x = y = z = 0.0;
	switch (n) {
	case 3: z = a[2];
	case 2: y = a[1];
	case 1: x = a[0];
		break;
	}
}


int Vector3::operator==(const Vector3& v)const
{
	return (x==v.x && y==v.y && z==v.z);
}

int Vector3::operator!=(const Vector3& v)const
{
	return (x!=v.x || y!=v.y || z!=v.z);
}
// Unary minus
Vector3 Vector3::operator-() {
	Vector3 v;
	v.x = -x; v.y = -y; v.z = -z;
	return v;
}

// Unary 2D perp operator
Vector3 Vector3::operator~() {
	Vector3 v;
	v.x = -y; v.y = x; v.z = z;
	return v;
}

// Scalar multiplication
Vector3 operator*( int c, const Vector3& w ) {
	Vector3 v;
	v.x = c * w.x;
	v.y = c * w.y;
	v.z = c * w.z;
	return v;
}

Vector3 operator*( float c,const Vector3& w ) {
	Vector3 v;
	v.x = c * w.x;
	v.y = c * w.y;
	v.z = c * w.z;
	return v;
}

Vector3 operator*( const Vector3& w, int c ) {
	Vector3 v;
	v.x = c * w.x;
	v.y = c * w.y;
	v.z = c * w.z;
	return v;
}

Vector3 operator*( const Vector3& w, float c ) {
	Vector3 v;
	v.x = c * w.x;
	v.y = c * w.y;
	v.z = c * w.z;
	return v;
}

// Scalar division
Vector3 operator/( const Vector3& w, int c ) {
	Vector3 v;
	v.x = w.x / c;
	v.y = w.y / c;
	v.z = w.z / c;
	return v;
}

Vector3 operator/( const Vector3& w, float c ) {
	Vector3 v;
	v.x = w.x / c;
	v.y = w.y / c;
	v.z = w.z / c;
	return v;
}
//------------------------------------------------------------------
//  Arithmetic Ops
//------------------------------------------------------------------

Vector3 Vector3::operator+(const Vector3& w )const  {
	Vector3 v;
	v.x = x + w.x;
	v.y = y + w.y;
	v.z = z + w.z;
	return v;
}


//------------------------------------------------------------------
//  Products
//------------------------------------------------------------------

// Inner Dot Product
float Vector3::operator*( const Vector3& w )const {
	return (x * w.x + y * w.y + z * w.z);
}


// 2D Exterior Perp Product
float Vector3::operator|( const Vector3& w )const {
	return (x * w.y - y * w.x);
}

// 3D Exterior Cross Product
Vector3 Vector3::operator^( const Vector3& w )const {
	Vector3 v;
	v.x = y * w.z - z * w.y;
	v.y = z * w.x - x * w.z;
	v.z = x * w.y - y * w.x;
	return v;
}
//------------------------------------------------------------------
//  Shorthand Ops
//------------------------------------------------------------------

Vector3& Vector3::operator*=( float c ) {        // vector scalar mult
	x *= c;
	y *= c;
	z *= c;
	return *this;
}

Vector3& Vector3::operator/=( float c ) {        // vector scalar div
	x /= c;
	y /= c;
	z /= c;
	return *this;
}

Vector3& Vector3::operator+=( const Vector3& w ) {        // vector increment
	x += w.x;
	y += w.y;
	z += w.z;
	return *this;
}

Vector3& Vector3::operator-=( const Vector3& w ) {        // vector decrement
	x -= w.x;
	y -= w.y;
	z -= w.z;
	return *this;
}

Vector3& Vector3::operator^=(const Vector3& w ) {        // 3D exterior cross product
	float ox=x, oy=y, oz=z;
	x = oy * w.z - oz * w.y;
	y = oz * w.x - ox * w.z;
	z = ox * w.y - oy * w.x;
	return *this;
}

//------------------------------------------------------------------
//  Special Operations
//------------------------------------------------------------------

void Vector3::normalize() {                      // convert to unit length
	float ln = sqrt( x*x + y*y + z*z );
	if (fabs(ln) < 1e-5f) return;						// do nothing for nothing
	ln = 1.0f / ln;
	x = x*ln;
	y = y*ln;
	z = z*ln;
}

Vector3 Vector3::cross( const Vector3& vec ) const
{
	return Vector3( y * vec.z - z * vec.y, z * vec.x - x * vec.z, x * vec.y - y * vec.x );
}
Vector3 Vector3::cross(  Vector3& vec1,   Vector3& vec2) 
{
	return Vector3( vec1.y * vec2.z - z * vec2.y, vec1.z * vec2.x - vec1.x * vec2.z, vec1.x * vec2.y - vec1.y * vec2.x );
}
float Vector3::dot( const Vector3& vec ) const
{
	return x * vec.x + y * vec.y + z * vec.z;
}

void Vector3::setValue( float _x,float _y,float _z )
{
	x=_x;
	y=_y;
	z=_z;
}


Vector3 sum( int n, int c[], Vector3 w[] ) {     // vector sum
	Vector3  v;
	for (int i=0; i<n; i++) {
		v.x += c[i] * w[i].x;
		v.y += c[i] * w[i].y;
		v.z += c[i] * w[i].z;
	}
	return v;
}

Vector3 sum( int n, float c[], Vector3 w[] ) {  // vector sum
	Vector3  v;

	for (int i=0; i<n; i++) {
		v.x += c[i] * w[i].x;
		v.y += c[i] * w[i].y;
		v.z += c[i] * w[i].z;
	}
	return v;
}